package com.glut.campusrepair.service.impl;

import com.glut.campusrepair.dto.FailureRepairdto;
import com.glut.campusrepair.entity.FailureRepair;
import com.glut.campusrepair.mapper.FailureRepairMapper;
import com.glut.campusrepair.service.IFailureRepairService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FailureRepairServiceImpl extends ServiceImpl<FailureRepairMapper, FailureRepair> implements IFailureRepairService {

    @Autowired
    FailureRepairMapper failureRepairMapper;

    @Override
    public FailureRepair getFailureRepairById(Long id) {
        return this.getById(id);
    }

    @Override
    public List<FailureRepair> getUnprocessedFailureRepairs() {
        LambdaQueryWrapper<FailureRepair> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FailureRepair::getRepairStatus, "待处理");
        return this.list(queryWrapper);
    }

    @Override
    public boolean updateStatus(Long id, String status) {
        FailureRepair failureRepair = new FailureRepair();
        failureRepair.setFailureRepairId(id.intValue()); // 显式转换
        failureRepair.setRepairStatus(status);
        return this.updateById(failureRepair);
    }


    @Override
    public List<FailureRepair> listByCondition(Map<String, Object> params) {
        LambdaQueryWrapper<FailureRepair> queryWrapper = new LambdaQueryWrapper<>();
        if (params.containsKey("faultDescription")) {
            queryWrapper.like(FailureRepair::getFaultDescription, params.get("faultDescription"));
        }
        if (params.containsKey("repairStatus")) {
            queryWrapper.eq(FailureRepair::getRepairStatus, params.get("repairStatus"));
        }
        return this.list(queryWrapper);
    }


    @Override
    public List<FailureRepair> getFailureRepairList() {
        // 使用LambdaQueryWrapper，即使为空，也会明确调用哪个list方法
        LambdaQueryWrapper<FailureRepair> queryWrapper = new LambdaQueryWrapper<>();
        return this.list(queryWrapper);
    }


    /**wsy
     * 根据故障报修ID查询相应报修的故障信息
     * 点击查看详情按钮后的操作
     * */
    @Override
    public FailureRepairdto selectFailureRepairById(Integer failureRepairId) {
        return failureRepairMapper.selectFailureRepairById(failureRepairId);
    }
}
